package com.lhn.netty.netty_demo.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author lhn
 * @date 2025/06/11 19:32
 * @since 1.0.0
 **/
public class MyServerHandler extends SimpleChannelInboundHandler <MessageProtocol> {
    private int count;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
        byte[] bytes = new byte[messageProtocol.getLength()];

        String string = new String(messageProtocol.getContent(), StandardCharsets.UTF_8);
        System.out.println("接收到的长度 : " + messageProtocol.getLength());
        System.out.println("服务器接收到消息 :   " + string);
        System.out.println("服务器接收消息数量 : " + (++count));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}